﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;
using API.Models;

namespace API.Controllers
{
    [RoutePrefix("api/Template")]
    public class TemplateController : ApiController
    {
        B_Template _bt;
        public TemplateController()
        {
            _bt = new B_Template();
        }

        /// <summary>
        /// 带参数的查询所有数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll(int page, int size)
        {
            B_TemplateClassify classify = new B_TemplateClassify();
            var data = from t in _bt.GetAll()
                       join c in classify.GetAll()
                       on t.ClassifyID equals c.ClassifyID
                       select new Param_Template
                       {
                           ClassifyID = c.ClassifyID,
                           Cname = c.Cname,
                           Describe = t.Describe,
                           Price = t.Price,
                           Style = t.Style,
                           TemplateID = t.TemplateID,
                           Thumbsup = t.Thumbsup,
                           Timg = t.Timg,
                           Tname = t.Tname
                       };
            return Ok(new Result<List<Param_Template>>
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }
        /// <summary>
        /// 不带参数的查询所有
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll()
        {
            var data = from i in _bt.GetAll()
                       orderby i.TemplateID ascending
                       select new Param_Template
                       {
                           TemplateID = i.TemplateID,
                           Tname = i.Tname,
                           Describe = i.Describe,
                           Timg = i.Timg,
                           Style = i.Style,
                           Price = i.Price,
                           Thumbsup = i.Thumbsup,
                       };
            return Ok(new Result<List<Param_Template>>
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 根据ID查一条单独的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("FindByID/{id}")]
        [HttpGet]
        public IHttpActionResult FindByID(int id)
        {
            try
            {
                var data = _bt.FindById(id);
                return Ok(new Result<Param_Template>
                {
                    Data = new Param_Template
                    {
                        TemplateID = data.TemplateID,
                        Tname = data.Tname,
                        Describe = data.Describe,
                        Timg = data.Timg,
                        Style = data.Style,
                        Price = data.Price,
                        Thumbsup = data.Thumbsup,
                        ClassifyID = data.ClassifyID
                    }
                });
            }
            catch (Exception)
            {
                return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpGet]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                _bt.Delete(id);
                return Ok(new Result<object> { });
            }
            catch (Exception)
            {
                return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(Param_Template template)
        {
            var data = HttpHelper.HttpPost<Result<string>>(Tools.ImgPath, "post", new
            {
                base64 = template.Timg,
                imgurl = "TemplateImg",
            });
            if (data.Code == 200)
            {
                try
                {
                    //添加这样写是为了图片
                    var aa = new Template
                    {
                        TemplateID = template.TemplateID,
                        Tname = template.Tname,
                        Describe = template.Describe,
                        Timg = data.Data,//图片
                        Style = template.Style,
                        Price = template.Price,
                        Thumbsup = template.Thumbsup,
                        ClassifyID = template.ClassifyID,
                    };
                    _bt.Add(aa);
                    return Ok(new Result<object>());
                }
                catch (Exception)
                {
                    return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
                }
            }
            return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        [Route("Update/{id}")]
        [HttpPost]
        public IHttpActionResult Update(int id, Param_Template template)
        {
            var data = HttpHelper.HttpPost<Result<string>>(Tools.ImgPath, "post", new
            {
                base64 = template.Timg,
                imgurl = "TemplateImg",
            });
            if (data.Code == 200) {
                try
                {
                    var aa = new Template
                    {
                        TemplateID = template.TemplateID,
                        Tname = template.Tname,
                        Describe = template.Describe,
                        Timg = data.Data,//图片
                        Style = template.Style,
                        Price = template.Price,
                        Thumbsup = template.Thumbsup,
                        ClassifyID = template.ClassifyID,
                    };
                    _bt.Update(id, aa);
                    return Ok(new Result<object>());
                }
                catch (Exception)
                {
                    return Ok(new Result<object> { Code = 500, Msg = "出现错误，请联系管理员" });
                }
            }
            else
            {
                return Ok(new Result<object> { Code = 500, Msg = "出现错误，请联系管理员" });
            }  
        }

        /// <summary>
        /// 通过模板来查所有和模板有关系的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetFullTemplate/{id}")]
        public IHttpActionResult GetFullTemplate(int id)
        {
            var data = _bt.GetFullTemplate(id);
            return Ok(new Result<IDAL.Full_Template> { Data=data});
        }
    }
}
